$(function () {
  getUserInfo()
  //结构样式
  layui.use(['element', 'layer', 'util'], function () {
    var element = layui.element,
      layer = layui.layer,
      util = layui.util,
      $ = layui.$;

    //头部事件
    util.event('lay-header-event', {
      //左侧菜单事件
      menuLeft: function (othis) {
        layer.msg('展开左侧菜单的操作', {
          icon: 0
        });
      },
      menuRight: function () {
        layer.open({
          type: 1,
          content: '<div style="padding: 15px;">处理右侧面板的操作</div>',
          area: ['260px', '100%'],
          offset: 'rt' //右上角
            ,
          anim: 5,
          shadeClose: true
        });
      }
    });

  });
  //退出
  $('.logout').on('click', function () {
    layer.confirm('确认退出登录吗？', {
      icon: 3,
      title: '提示'
    }, function (index) {
      localStorage.removeItem('token')
      location.href = '/login.html'
      layer.close(index);
    });
  })
})
//获取
function getUserInfo() {
  axios({
    url: '/my/userinfo',
    method: 'get',
    // headers: {
    //   Authorization: localStorage.getItem('token')
    // }

  }).then(({
    data: res
  }) => {
    //成功回调
    renderAvatar(res.data)
    // console.log(res)
  })
}
//个人信息渲染
function renderAvatar(use) {
  let name = use.nickname || use.username
  $('.welcome').text(`欢迎 ${name}`)
  if (use.user_pic != null) {
    $('.layui-nav-img').show().prop('src',use.user_pic)
    $('.axatar-text').hide()
  } else {
    $('.axatar-text').show().text(name[0].toUpperCase())
    $('.layui-nav-img').hide()
  }
}